home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 09 Laramée / Simulation.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-08-22  |  4.9 KB  |  204 lines

  1. /***************************************************************
  2.  * Implementation of the Simulation Class
  3.  *************************************************************/
  4.  
  5. #include "Simulation.h"
  6. #include <stdlib.h>
  7. #include "WorldGrid.h"
  8. #include <iostream.h>
  9.  
  10. Simulation::Simulation( int x, int y )
  11. {
  12.     for( int i = 0; i < 3; i++ )
  13.     {
  14.         Grids[ i ] = new WorldGrid( x, y );
  15.     }
  16. }
  17.  
  18. Simulation::Simulation()
  19. {
  20.     for( int i = 0; i < 3; i++ )
  21.     {
  22.         Grids[ i ] = new WorldGrid();
  23.     }
  24. }
  25.  
  26. /****************************************************************
  27.  * Test case construction
  28.  ****************************************************************/
  29.  
  30. // We'll test each troll against three different test cases, each
  31. // of which has a unique combination of sheep population, number
  32. // of prowling knights, etc.
  33.  
  34. void Simulation::BuildTestCases()
  35. {
  36.     int i;
  37.     int x, y;
  38.  
  39.     // Let's start with a sheep-heavy case
  40.     Entity::AttachGrid( *Grids[ 0 ] );
  41.     for( i = 0; i < 60; i++ )
  42.     {
  43.         x = rand() % WORLD_GRID_SIZE;
  44.         y = rand() % WORLD_GRID_SIZE;
  45.         EntityTable[ 0 ][ i ] = new Sheep( x, y, i );
  46.     }
  47.     for( i = 60; i < 75; i++ )
  48.     {
  49.         x = rand() % WORLD_GRID_SIZE;
  50.         y = rand() % WORLD_GRID_SIZE;
  51.         EntityTable[ 0 ][ i ] = new Knight( x, y, i );
  52.     }
  53.     for( i = 75; i < 90; i++ )
  54.     {
  55.         x = rand() % WORLD_GRID_SIZE;
  56.         y = rand() % WORLD_GRID_SIZE;
  57.         EntityTable[ 0 ][ i ] = new Haven( x, y, i );
  58.     }
  59.     for( i = 90; i < 95; i++ )
  60.     {
  61.         x = rand() % WORLD_GRID_SIZE;
  62.         y = rand() % WORLD_GRID_SIZE;
  63.         EntityTable[ 0 ][ i ] = new Trap( x, y, i );
  64.     }
  65.     for( i = 95; i < MAX_ENTITIES; i++ )
  66.     {
  67.         x = rand() % WORLD_GRID_SIZE;
  68.         y = rand() % WORLD_GRID_SIZE;
  69.         EntityTable[ 0 ][ i ] = new Tower( x, y, i );
  70.     }
  71.  
  72.     // Now, a knight-heavy one
  73.     Entity::AttachGrid( *Grids[ 1 ] );
  74.     for( i = 0; i < 30; i++ )
  75.     {
  76.         x = rand() % WORLD_GRID_SIZE;
  77.         y = rand() % WORLD_GRID_SIZE;
  78.         EntityTable[ 1 ][ i ] = new Sheep( x, y, i );
  79.     }
  80.     for( i = 30; i < 75; i++ )
  81.     {
  82.         x = rand() % WORLD_GRID_SIZE;
  83.         y = rand() % WORLD_GRID_SIZE;
  84.         EntityTable[ 1 ][ i ] = new Knight( x, y, i );
  85.     }
  86.     for( i = 75; i < 90; i++ )
  87.     {
  88.         x = rand() % WORLD_GRID_SIZE;
  89.         y = rand() % WORLD_GRID_SIZE;
  90.         EntityTable[ 1 ][ i ] = new Haven( x, y, i );
  91.     }
  92.     for( i = 90; i < 95; i++ )
  93.     {
  94.         x = rand() % WORLD_GRID_SIZE;
  95.         y = rand() % WORLD_GRID_SIZE;
  96.         EntityTable[ 1 ][ i ] = new Trap( x, y, i );
  97.     }
  98.     for( i = 95; i < MAX_ENTITIES; i++ )
  99.     {
  100.         x = rand() % WORLD_GRID_SIZE;
  101.         y = rand() % WORLD_GRID_SIZE;
  102.         EntityTable[ 1 ][ i ] = new Tower( x, y, i );
  103.     }
  104.     
  105.     // And a relatively sparsely populated one to finish
  106.     Entity::AttachGrid( *Grids[ 2 ] );
  107.     for( i = 0; i < 20; i++ )
  108.     {
  109.         x = rand() % WORLD_GRID_SIZE;
  110.         y = rand() % WORLD_GRID_SIZE;
  111.         EntityTable[ 2 ][ i ] = new Sheep( x, y, i );
  112.     }
  113.     for( i = 20; i < 35; i++ )
  114.     {
  115.         x = rand() % WORLD_GRID_SIZE;
  116.         y = rand() % WORLD_GRID_SIZE;
  117.         EntityTable[ 2 ][ i ] = new Knight( x, y, i );
  118.     }
  119.     for( i = 35; i < 45; i++ )
  120.     {
  121.         x = rand() % WORLD_GRID_SIZE;
  122.         y = rand() % WORLD_GRID_SIZE;
  123.         EntityTable[ 2 ][ i ] = new Trap( x, y, i );
  124.     }
  125.     for( i = 45; i < 50; i++ )
  126.     {
  127.         x = rand() % WORLD_GRID_SIZE;
  128.         y = rand() % WORLD_GRID_SIZE;
  129.         EntityTable[ 2 ][ i ] = new Tower( x, y, i );
  130.     }
  131.     for( i = 50; i < 55; i++ )
  132.     {
  133.         x = rand() % WORLD_GRID_SIZE;
  134.         y = rand() % WORLD_GRID_SIZE;
  135.         EntityTable[ 2 ][ i ] = new Tower( x, y, i );
  136.     }    
  137.     for( i = 55; i < MAX_ENTITIES; i++ )
  138.     {
  139.         x = rand() % WORLD_GRID_SIZE;
  140.         y = rand() % WORLD_GRID_SIZE;
  141.         EntityTable[ 2 ][ i ] = new Haven( x, y, i );
  142.     }
  143. }
  144.  
  145.  
  146. /****************************************************************
  147.  * Testing a Troll
  148.  ***************************************************************/
  149.  
  150. double Simulation::RunSim( Troll & troll )
  151. {
  152.     double results[ 3 ];
  153.  
  154.     // Run the three test cases...
  155.     for( int i = 0; i < 3; i++ )
  156.     {
  157.         // Start each test by making a working copy of the world data
  158.         troll.Reset();
  159.         WorkingGrid.Copy( Grids[ i ] );
  160.         for( int j = 0; j < MAX_ENTITIES; j++ )
  161.         {
  162.           if( EntityTable[ i ][ j ] != NULL )
  163.                 WorkingTable[ j ] = EntityTable[ i ][ j ]->Clone();
  164.             else
  165.                 WorkingTable[ j ] = NULL;
  166.         }
  167.  
  168.         Entity::AttachGrid( WorkingGrid );
  169.         Entity::AttachTroll( troll );
  170.  
  171.         // Run the simulation until the troll dies or time runs out
  172.         int time = 0;
  173.         while( time < 500 )
  174.         {
  175.             // Let each entity (troll included) perform its turn
  176.             for( j = 0; j < MAX_ENTITIES; j++ )
  177.             {
  178.                 if ( WorkingTable[ j ] != NULL )
  179.                     WorkingTable[ j ]->Update();
  180.             }
  181.             troll.Update();
  182.  
  183.             // Short-circuit the sim if the troll dies
  184.             if( troll.Dead() )
  185.                 break;
  186.  
  187.             time++;
  188.         }
  189.  
  190.  
  191.         // Obtain the results
  192.         results[ i ] = troll.GetEvaluation();
  193.  
  194.         // And clean up
  195.         for( j = 0; j < MAX_ENTITIES; j++ )
  196.         {
  197.           if( WorkingTable[ j ] != NULL )
  198.                 delete( WorkingTable[ j ] );
  199.         }
  200.     }
  201.  
  202.     // And report the results...
  203.     return( ( results[ 0 ] + results[ 1 ] + results[ 2 ] ) / 3.0 );
  204. }